<template>
  <cube-scroll ref="scroll" class="ratings">
    <div class="ratings-content">
      <div class="overview">
        <div class="overview-left">
          <h1 class="score">{{ seller.score }}</h1>
          <div class="title">综合评分</div>
          <div class="rank">高于周边商家%</div>
        </div>
        <div class="overview-right">
          <div class="score-wrapper">
            <span class="title">服务态度</span>
            <cube-rate v-model="attitude"></cube-rate>
            <span class="score">{{ seller.serviceScore }}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">商品评分</span>
            <cube-rate v-model="score"></cube-rate>
            <span class="score">{{ seller.foodScore }}</span>
          </div>
          <div class="delivery-wrapper">
            <span class="title">送达时间</span>
            <span class="delivery">{{ seller.deliveryTime }}分钟</span>
          </div>
        </div>
      </div>
      <div class="rating-wrapper">
        <div class="rating-type border-bottom-1px">
          <ins
            v-for="(item, index) of this.allComment"
            :key="item.type"
            @click="tabContents(index)"
          >
            <!-- 全部？ -->
            <span
              class="block positive"
              v-if="item.type === 1"
              :class="{ active: index == isShow }"
            >
              <span class="count"
                >{{ item.title }} {{ item.content.length }}</span
              >
            </span>
            <!-- 满意 -->
            <span
              class="block positive"
              v-else-if="item.type === 2"
              :class="{ active: index == isShow }"
            >
              <span class="count"
                >{{ item.title }} {{ item.content.length }}</span
              >
            </span>
            <!-- 不满意 -->
            <span
              class="block negative"
              v-else-if="item.type === 3"
              :class="{ active: index == isShow }"
            >
              <span class="count"
                >{{ item.title }} {{ item.content.length }}</span
              >
            </span>
          </ins>
        </div>

        <div class="switch">
          <cube-checkbox v-model="checked" @input="cotChange"
            >只看有内容的评价</cube-checkbox
          >
        </div>
        <!-- 评论内容 -->
        <ul>
          <li
            class="rating-item border-bottom-1px"
            v-for="item of this.contents"
            :key="item.index"
          >
            <div class="avatar">
              <img width="28" height="28" :src="item.avatar" />
            </div>
            <div class="content">
              <h1 class="name">{{ item.username }}</h1>
              <div class="star-wrapper">
                <cube-rate v-model="item.score"></cube-rate>
                <span class="delivery">{{ item.deliveryTime }}</span>
              </div>
              <p class="text">{{ item.text }}</p>
              <div class="recommend">
                <i
                  class="cubeic-good"
                  v-show="
                    item.recommend.length === 0 || item.recommend === []
                      ? false
                      : true
                  "
                ></i>
                <span
                  class="item"
                  v-for="recommend of item.recommend"
                  :key="recommend.index"
                  >{{ recommend }}</span
                >
              </div>

              <div class="time"></div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </cube-scroll>
</template>

<script>
import { getVal } from '../api';
export default {
  name: 'ratings',

  data() {
    return {
      attitude: 0,
      score: 0,
      // starScore: 2,
      seller: {},

      checked: false,
      isShow: -1,
      ratings: [],
      contents: {},
      allComment: [
        { type: 1, title: '全部', content: [] },
        { type: 2, title: '满意', content: [] },
        { type: 3, title: '不满意', content: [] },
      ],
    };
  },
  props: {
    data: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  watch: {
    ratings: {
      handler: function(newVal) {
        this.ratings = newVal;
        this.satisfaction();
      },
      deep: true, //深度监听
      immediate: true, //监听后立即执行函数里的方法
    },
    seller: {
      handler: function(newVal) {
        this.attitude = newVal.serviceScore;
        this.score = newVal.foodScore;
      },
      deep: true,
      immediate: true,
    },
  },
  created() {
    this.allEle();
  },
  methods: {
    async allEle() {
      const result = await getVal();
      try {
        this.ratings = result.data.ratings;
        this.seller = result.data.seller;
      } catch (error) {
        console.log(error);
      }
    },
    satisfaction() {
      const _ratings = this.ratings;
      this.allComment[0].content = _ratings;
      this.allComment[1].content = _ratings.filter((item) => item.score >= 4);
      this.allComment[2].content = _ratings.filter((item) => item.score < 4);
      this.contents = this.allComment[0].content;
    },
    //点击tab,x显示对应的内容
    tabContents(index) {
      // this.checked = false;    ]

      this.isShow = index;
      if (this.checked) {
        this.contents = this.allComment[index].content.filter(
          (item) => item.text
        );
      } else {
        this.contents = this.allComment[index].content;
      }
    },

    cotChange(e) {
      if (e) {
        this.contents = this.contents.filter((item) => item.text);
        console.log(this.contents.filter((item) => item.text));
      } else {
        this.contents = this.allComment[this.isShow].content;
      }
    },

    //点击tab,x显示对应的内容
    tabContent(type, index, check) {
      index ? index : 0;
      this.isShow = index;
      this.contents = [];
      if (check) {
        this.checked = true;

        if (type === undefined) {
          this.contents = this.allComment[0].content;
        } else {
          console.log('type', type - 1);
          this.contents = [];
          const index = type - 1;
          this.contents = this.allComment[index].text.filter(
            (item) => item.text
          );
        }
      } else {
        if (type === undefined) {
          this.contents = this.allComment[0].content;
        } else {
          const index = type - 1;
          this.contents = this.allComment[index].content;
        }
      }
    },
  },
};
</script>

<style lang="stylus" scoped>
.ratings {
  position: relative;
  text-align: left;
  white-space: normal;
  height: 100%;

  .overview {
    display: flex;
    padding: 18px 0;

    .overview-left {
      flex: 0 0 137px;
      padding: 6px 0;
      width: 137px;
      border-right: 1px solid #d9dde1;
      text-align: center;

      @media only screen and (max-width: 320px) {
        flex: 0 0 120px;
        width: 120px;
      }

      .score {
        margin-bottom: 6px;
        line-height: 28px;
        font-size: 24px;
        color: #fc9153;
      }

      .title {
        margin-bottom: 8px;
        line-height: 12px;
        font-size: 12px;
        color: #333;
      }

      .rank {
        line-height: 10px;
        font-size: 12px;
        color: #999;
      }
    }

    .overview-right {
      flex: 1;
      padding: 6px 0 6px 24px;

      @media only screen and (max-width: 320px) {
        padding-left: 6px;
      }

      .score-wrapper {
        display: flex;
        align-items: center;
        margin-bottom: 8px;

        .title {
          line-height: 18px;
          font-size: 12px;
          color: #333;
        }

        .star {
          margin: 0 12px;
        }

        .score {
          line-height: 18px;
          font-size: 12px;
          color: #fc9153;
        }
      }

      .delivery-wrapper {
        display: flex;
        align-items: center;

        .title {
          line-height: 18px;
          font-size: 12px;
          color: #333;
        }

        .delivery {
          margin-left: 12px;
          font-size: 12px;
          color: #999;
        }
      }
    }
  }

  .rating-wrapper {
    padding: 0 18px;

    .rating-type {
      .block {
        display: inline-block;
        padding: 8px 12px;
        margin-right: 8px;
        line-height: 16px;
        border-radius: 1px;
        font-size: 12px;
        color: $color-grey;

        &.active {
          color: #fff;
        }

        .count {
          margin-left: 2px;
        }

        &.positive {
          background: rgba(0, 160, 220, 0.2);

          &.active {
            background: #00a0dc;
          }
        }

        &.negative {
          background: #ccc;

          &.active {
            background: $color-grey;
          }
        }
      }
    }

    .rating-item {
      display: flex;
      padding: 18px 0;

      &:last-child {
        border-none();
      }

      .avatar {
        flex: 0 0 28px;
        width: 28px;
        margin-right: 12px;

        img {
          height: auto;
          border-radius: 50%;
        }
      }

      .content {
        position: relative;
        flex: 1;

        .name {
          margin-bottom: 4px;
          line-height: 12px;
          font-size: 12px;
          color: #333;
        }

        .star-wrapper {
          margin-bottom: 6px;
          display: flex;
          align-items: center;

          .star {
            margin-right: 6px;
          }

          .delivery {
            font-size: 12px;
            color: #999;
          }
        }

        .text {
          margin-bottom: 8px;
          line-height: 18px;
          color: #333;
          font-size: 12px;
        }

        .recommend {
          display: flex;
          align-items: center;
          flex-wrap: wrap;
          line-height: 16px;

          .cubeic-good {
            color: #00a0dc;
            font-size: 12px;
            margin: 0 8px 4px 0;
          }

          .item {
            padding: 0 6px;
            border: 1px solid rgba(7, 17, 27, 0.1);
            border-radius: 1px;
            color: #999;
            background: #fff;
            font-size: 12px;
            margin: 0 8px 4px 0;
          }
        }

        .time {
          position: absolute;
          top: 0;
          right: 0;
          line-height: 12px;
          font-size: 12px;
          color: #999;
        }
      }
    }
  }
}
</style>
